<?php
// +----------------------------------------------------------------------
// | 万能表单解析实现
// +----------------------------------------------------------------------
// | XYCMS 逍遥CMS
// +----------------------------------------------------------------------
// | 作者：Rakiy
// +----------------------------------------------------------------------
// | 开源协议 ( https://mit-license.org )
// +----------------------------------------------------------------------

class Form extends Think {
    public $data = array() , $isadmin=1, $doThumb=1, $doAttach=1;
    public function __construct($data=array()) {
         $this->data = $data;
    }
    public function catid($info,$value){
        return;  
    }
    public function title($info,$value){
        return;
    }
    /* 文本框 */
    public function text($info,$value){
        $info['setup'] = is_array($info['setup']) ? $info['setup'] : json_decode($info['setup'],true);
        $id = $field = $info['field'];
        $validate = $this->getvalidate($info['setup']);
        $info['setup']['ispassword'] ? $inputtext = 'password' : $inputtext = 'text';
        $parseStr   = ' <input type="' . $inputtext . '" name="' . $field . '" placeholder="' . $info['setup']['default'] . '" value="' . stripcslashes($value) . '" class="layui-input" ' . $validate . '><p class="help-block">'.$info['setup']['remark'].'</p>';
        return $parseStr;
    }
    /* 密码框 */
    public function password($info,$value){
        $info['setup'] = is_array($info['setup']) ? $info['setup'] : json_decode($info['setup'],true);
        $id = $field = $info['field'];
        $validate = $this->getvalidate($info['setup']);
        $info['setup']['ispassword'] ? $inputtext = 'password' : $inputtext = 'text';
        $parseStr   = ' <input type="' . $inputtext . '" name="' . $field . '" placeholder="' . $info['setup']['default'] . '" value="' . stripcslashes($value) . '" class="layui-input" ' . $validate . '><p class="help-block">'.$info['setup']['remark'].'</p>';
        return $parseStr;
    }
    /* 坐标框 */
    public function point($info,$value){
        $info['setup'] = is_array($info['setup']) ? $info['setup'] : json_decode($info['setup'], true);
        $id = $field = $info['field'];
        $validate = $this->getvalidate($info['setup']);
        $parseStr   = ' <input type="text" name="' . $field . '" data-map="point" data-name="' . $field . '" data-title="坐标选择" placeholder="' . $info['setup']['default'] . '" value="' . stripcslashes($value) . '" class="layui-input" ' . $validate . '><p class="help-block">' . $info['setup']['default'] . '</p>';
        return $parseStr;
    }
    /*文本域*/
    public function textarea($info,$value){
        $info['setup'] = is_array($info['setup']) ? $info['setup'] : json_decode($info['setup'],true);
        $id = $field = $info['field'];
        $validate = $this->getvalidate($info['setup']);
        $rows = isset($info['setup']['rows']) ? intval($info['setup']['rows']) : 10;
        $cols = isset($info['setup']['rows']) ? intval($info['setup']['cols']) : 185;
        $parseStr   = '<textarea style="height:80px;resize:none;line-height:20px" class="layui-input" placeholder="' . $info['setup']['default'] . '" name="'.$field.'"  rows="'. $rows .'" cols="' . $cols . '"  id="'.$id.'"   '.$validate.' data-value="'.stripcslashes($value).'"/></textarea><p class="help-block">'.$info['setup']['remark'].'</p>';
        return $parseStr;
    }
    /*单选按钮实现*/
    public function radio($info,$value){
        $info['setup'] = is_array($info['setup']) ? $info['setup'] : json_decode($info['setup'],true);
        $id = $field = $info['field'];
        $validate = $this->getvalidate($info['setup']);
        if(!isset($info['setup']['option']) || empty(trim($info['setup']['option']))){
            return false;
        }
        $options  = explode('|' , trim($info['setup']['option']));
        $parseStr = "";
        foreach($options as $k=>$v){
            $checked = $k == $value ? 'checked' : '';
            $parseStr .= '<label class="layui-form-label"><input type="radio" name="' . $field . '" value="' . $k . '" ' . $checked . '>' . $v . '</label>';
        }
        $parseStr .= '<p class="help-block">'.$info['setup']['remark'].'</p>';
        return $parseStr;
    }
    /*下拉列表实现*/
    public function select($info,$value){
        $info['setup'] = is_array($info['setup']) ? $info['setup'] : json_decode($info['setup'],true);
        $id = $field = $info['field'];
        $validate = $this->getvalidate($info['setup']);
        if(!isset($info['setup']['option']) || empty(trim($info['setup']['option']))){
            return false;
        }
        $options  = explode('|' , trim($info['setup']['option']));
        $parseStr = '<select name="' . $field . '" class="layui-input">';
        foreach($options as $k=>$v){
            $selected = $k == $value ? 'selected' : '';
            $parseStr .= '<option value="' . $v . '" ' . $selected . '>' . $v . '</option>' . "\n\r";
        }
        $parseStr .= '</select><p class="help-block">'.$info['setup']['remark'].'</p>';
        return $parseStr;
    }
    /* 验证实现 */
    function getvalidate($info){
        $validate_data = array();
        if(isset($info['required'])){
            $required  = ' required = "' . $info['required'] . '" '; 
        }
        if(isset($info['errormsg'])){
            $errormsg = ' title="'.$info['errormsg'].'"';
        }
        unset($info['required'], $info['errormsg'], $info['default'], $info['option'], $info['remark']);
        foreach ($info as $k => $v) {
            $validate_data[] = ' ' . $k . ':' . $v;
        }
        $validate = ltrim(implode(',',$validate_data), ' ');
        $validate = $validate ? 'validate="'.$validate.'" ' : '';
        return $required . $validate . $errormsg;
    }
}
?>